package com.eqxiu.ieditor.api.impl.order;

import java.util.Map;
import java.util.TreeMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import com.alibaba.dubbo.config.annotation.Service;
import com.eqxiu.ieditor.api.order.IChargeOrderServiceApi;
import com.eqxiu.ieditor.api.order.IOrderServiceApi;
import com.eqxiu.ieditor.commons.dto.BaseResultData;
import com.eqxiu.ieditor.commons.web.MsgCode;
import com.eqxiu.ieditor.commons.web.ResultData;
import com.eqxiu.ieditor.dto.OrderDTO;

import eqxiu.mall.product.service.api.ProductServiceApi;

/**
 * @description 直冲订单service<br/>
 * 本类为重构后的结果，原代码写在ChargeApiController中
 * @creatTime 2019年4月2日
 * @since 1.0.0
 */
@Service
public class ChargeOrderServiceApiImpl implements IChargeOrderServiceApi, BaseResultData {

	private final Logger logger = LoggerFactory.getLogger(ChargeOrderServiceApiImpl.class);

	// 绑定支付系统的数量
	private final Integer amount = 1;
	// 代表样例模板
	private final int SCENE_ATTR_GROUP_ID = 2;
	private final int PAYWAY_WECHAT = 0;

	@Autowired
	private IOrderServiceApi orderServiceApi;

	@Autowired
	private ProductServiceApi productService;
	
//	@Value("${payment.appkey}")
//	private String appKey;

	@Override
	public ResultData createOrder(OrderDTO orderDTO)  {
		// 1、校验
		Integer totalFee = orderDTO.getTotalFee();
		if (totalFee <= 0) {
			return success().setCode(MsgCode.CONSTRAINT_VIOLATION).setMsg("total fee invalid");
		}

		// 2、赋值
		orderDTO.setCreateUser(orderDTO.getUserId());
		orderDTO.setName(orderDTO.getName());
		handleMallProd(orderDTO, orderDTO.getProductCode());
		
		com.eqxiu.ieditor.dto.OrderDTO toSaveDTO = new com.eqxiu.ieditor.dto.OrderDTO();
		BeanUtils.copyProperties(orderDTO, toSaveDTO);
		
		// 3、持久化
		orderServiceApi.createOrder(toSaveDTO);
		orderDTO.setId(toSaveDTO.getId());// 坑爹：函数orderServiceApi.createOrder()的入参当做出参使用
		
		// 4、封装结果并返回
		Map<String, Object> map = new TreeMap<String, Object>();
		map.put("goodsId", orderDTO.getGoodsId());
		map.put("amount", amount);
		map.put("price", totalFee);
		map.put("remark", orderDTO.getRemark());
		map.put("orderAppId", orderDTO.getPaymentAppId());
		map.put("orderTradeId", toSaveDTO.getId());
		map.put("payway", PAYWAY_WECHAT);
		map.put("productCode", orderDTO.getPaymentProductCode());
		if (orderDTO.getNotifyUrl() == null || orderDTO.getNotifyUrl().equals("")) {
			map.put("notifyUrl", orderDTO.getPaymentNotifyCharge());
		} else {
			map.put("notifyUrl", orderDTO.getNotifyUrl());
		}
		String sign = orderServiceApi.signParam(map, orderDTO.getAppKey());
		map.put("sign", sign);

		return success().setObj(orderDTO).setMap(map);
	}

	private void handleMallProd(OrderDTO orderDTO, String code) {
		if (code != null) {
			Map<String, Object> prod = productService.getProductLiteByCodeAndAttrGroupId(code, SCENE_ATTR_GROUP_ID);
			if (prod != null) {
				Integer prodId = Integer.parseInt(prod.get("id").toString());
				orderDTO.setProductId(prodId);
			}
		}
	}

}
